#include "./include/test_hardware.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include "./include/io_test.h"
#include "./include/protocol.h"
#include "./include/tcp_sxx.h"

void test_start_bit(int cmd_fd, char* sendbuf)
{
    int ret = 0;
    int valread;
    int count = 0;
    unsigned short len = 0;
    unsigned char buffer[BUFFER_SIZE];
    unsigned char get_buf[BUFFER_SIZE];

    printf("等待[上电Bit]\n" );

    recv_tcp_data_discard( cmd_fd, 1 );
    count = 0;   
    
    while(1)
    {
        usleep(100000);  //1s
        valread = recv(cmd_fd, buffer, BUFFER_SIZE, 0);

        if(valread > 0)
        {
            if( buffer[6] == Type_Start_Bit )
            {
                if( count++ > 50 )
                {
                    printf("未接收到正确数据!\n");
                    return ;
                }
                break;
            }
        }
        if( count++ > 50 )
        {

            printf("未接收到正确数据!\n");
            return ;
        }
    }

    len = buffer[5] << 8 | buffer[4];
    printf("成功接收数据 接收数据长度:%d 数据:", len);
    for (int j = 0; j < len; j++) 
    {
        // 以大写十六进制格式输出，宽度为2，不足补0
        printf("0x%02X ", buffer[j]); 
    }
    printf("\n");
    handle_tcp_recv_package(len,buffer,0);
    
    return ;
}

void test_hardware(int cmd_fd, char* sendbuf)
{
    int ret = 0;
    int valread;
    int count = 0;
    unsigned short len = 0;
    unsigned char buffer[BUFFER_SIZE];
    unsigned char get_buf[BUFFER_SIZE];

    for( int i=2; i<=6; i++ )
    {
        if(i == Type_Flash)
        {
            continue;
        }
        printf("\n");
        switch(i)
        {
            case Type_Sycle_Bit:
                printf("开始测试[周期Bit]\n" );
                break;
            case Type_DDR3:
                printf("开始测试[DDR3]\n" );
                break;
            case Type_Flash:
                printf("开始测试[Flash]\n" );
                break;
            case Type_Nvsram:
                printf("开始测试[Nvsram]\n" );
                break;
            case Type_Disk:
                printf("开始测试[电子盘]\n" );
                break;
            default :
                printf("功能类型不正确!\n");
                break;
        }
        recv_tcp_data_discard( cmd_fd, 1 );
        count = 0;   
        
        while(1)
        {
            usleep(50000);  //1s
            valread = recv(cmd_fd, buffer, BUFFER_SIZE, 0);
 
            if(valread > 0)
            {
                if( buffer[6] == i )
                {
                    count++;
                    break;
                }
            }
            if( count++ > 100 )
            {

                printf("未接收到正确数据!\n");
                break ;
            }
        }
        if(count > 50)
        {
            continue;
        }

        len = buffer[5] << 8 | buffer[4];
        printf("成功接收数据 接收数据长度:%d 数据:", len);
        for (int j = 0; j < len; j++) 
        {
            // 以大写十六进制格式输出，宽度为2，不足补0
            printf("0x%02X ", buffer[j]); 
        }
        printf("\n");
        handle_tcp_recv_package(len,buffer,0);
    }
    return ;
}